<?php

class Note
{
	private $id = -1; // note id
	
	private $title = ""; // Title
	private $author = ""; // Author
	private $body = ""; // body
	private $cdate = ""; // Date of creation
	private $mdate = ""; // Date of last modification
	private $hash = ""; // Unique hash
	private $excerpt = ""; // Summary
	private $link = ""; // url
	private $category_id = null; // Category
	private $used = 0; // used, cited ? (boolean)
	private $udate = null; // date used
	
	public $display = true;
			
	// Constructor
	function __construct($id = -1, $title = '', $body = '', $author = '', $cdate = '', $mdate = '', $category_id = '', $link='', $used = 0, $udate=null)
	{
		if($id > 0) $this->id = $id;
		if(!empty($title)) $this->title = $title;
		if(!empty($author)) $this->author = $author;
		if(!empty($body)) $this->body = $body;
		if(!empty($cdate)) $this->cdate = $cdate;
		if(!empty($mdate)) $this->mdate = $mdate;
		if(!empty($category_id)) $this->category_id = $category_id;
		if(!empty($link)) $this->link = $link;
		if(!empty($used)) $this->used = $used;
		if(!empty($udate)) $this->udate = $udate;
		$this->makeHash();
		$this->makeExcerpt();
	}
	
	##########################
	# Loads notes from the database and returns Note objects
	# Use : $note = Note::load($note_id) // loads 1 note identified by its ID, returns a Note object
	# Use : $notes = Note::load("all") // loads all notes and returns an array of Note objects
	# Param : id, limit, param ('before' or 'after')
	# Returns : object Note (single) or object NoteSet (range of notes or all notes)
	public static function load($id = "all", $limit="none", $param = "none")
	{
		$db = new Database();
		
		// all notes
		if(!is_numeric($id) or $id == "all")
		{
			$ns = new NoteSet();
			$query = $db->select("notes");
			while($data = $db->fetch())
			{
				$ns->append(Note::load($data['id']));
			}
			return $ns;
		}
		// one note, identified by its id
		elseif(is_numeric($id) and $limit == "none")
		{
			$ar['id'] = $id;
		
			$db->select("notes",'*',$ar, '1');
			$data = $db->fetch();
		
			return new Note($id, $data['title'], $data['body'], 
			$data['author'], $data['cdate'], $data['mdate'], $data['category_id'], $data['link'],
			$data['used'],$data['udate']);
		}
		// a range of notes (starting from id)
		elseif(is_numeric($id) and is_numeric($limit) and $param == "none")
		{
			$ns = new NoteSet();
			$query = $db->select("notes", '*', 1, $id.','.$limit);
			while($data = $db->fetch())
			{
				$ns->append(Note::load($data['id']));
			}
			return $ns;
		}
	}
		
	##########################
	# Creates a NEW note and injects it into the database
	# Use : 1) set the variables with setter methods, 2) call this method
	# Param :
	# Returns :
	public function create()
	{
		$data['id'] = '';
		$data['title'] = $this->getTitle();
		$data['author'] = $this->getAuthor();
		$data['body'] = $this->getBody();
		$data['excerpt'] = $this->makeExcerpt();
		$data['cdate'] = $this->cdate = "now()";
		$data['mdate'] = $this->mdate = "now()";
		$data['hash'] = $this->makeHash();
		$data['category_id'] = $this->getCategoryId();
		$data['link'] = $this->getLink();
		$data['used'] = $this->isUsed();
		$data['udate'] = $this->getUsageDate();
		
		$db = new Database();
		
		// Hash control : do not create a note if it already exists !
		$db->query("SELECT * FROM notes WHERE hash='".$data['hash']."'");
		if($db->num_rows() > 0)
			return false;
		
		return $db->insert("notes",$data);
	}
	
	##########################
	# Updates the current note in the database
	# Use : 1) set the variables with setter methods, 2) call this method
	# e.g. $note->save() // saves all fields in $note instance
	# Use : 1) set one variable with its setter, 2) call this method with the name of the field
	# e.g. : $note->save('title') // saves only 'title' field in $note instance
	# Param :
	# Returns :
	public function save($field = "")
	{
		// Save all fields
		if(empty($field))
		{
			//$data['id'] = '';
			$data['title'] = $this->getTitle();
			$data['author'] = $this->getAuthor();
			$data['body'] = $this->getBody();
			$data['excerpt'] = $this->makeExcerpt();
			$data['cdate'] = $this->cdate = "now()";
			$data['mdate'] = $this->mdate = "now()";
			$data['category_id'] = $this->getCategoryId();
			$data['link'] = $this->getLink();
			$data['used'] = $this->isUsed();
			$data['udate'] = $this->getUsageDate();
			
			$db = new Database();
			return $db->update("notes",$data);
		}
		// Save one field
		else
		{
			$getField = 'get'.ucfirst($field);
			$val = $this->$getField();
			$data[$field] = $val;
			$data['mdate'] = $this->mdate = "now()";
			$db = new Database();
			return $db->update("notes",$data,"id='".$this->getId()."'");
		}
	}
	
	##########################
	# Prints the value of the given variable
	# Param : the name of one of the class variables (e.g. 'title' or 'body')
	# Returns :
	public function show($element)
	{
		switch($element)
		{
			case 'title':	echo $this->getTitle(); break;
			case 'body':	echo $this->getBody(); break;
			case 'excerpt': echo $this->makeExcerpt(); break;
			case 'author':	echo $this->getAuthor(); break;
			case 'id':		echo $this->getId(); break;
			case 'creation':echo $this->getCreationDate(); break;
			case 'creation_f': echo Database::getFrenchDate($this->getCreationDate()); break;
			case 'update':	echo $this->getModificationDate(); break;
			case 'hash':	echo $this->getHash(); break;
			case 'category_id': echo $this->getCategoryId(); break;
			case 'category':	echo $this->getCategory(); break;
			case 'link':	echo $this->getLink(); break;
			case 'linked-title': echo "<a href=\"".$this->getLink()."\">".$this->getTitle()."</a>";
			default: echo ""; break;
		}
	}

	
	##########################
	# Setters
	
	public function setTitle($title) { $this->title = $title; }
	public function setAuthor($author) { $this->author = $author; }
	public function setBody($body) { $this->body = $body; }
	public function setCategoryId($id) { $this->category_id = $id; }
	public function setLink($url) { $this->link = $url; }
	public function setUsed($u=1) { $this->used = $u; }
	public function setUsageDate($d=null) { if(is_null($d)) $d = date("Y-m-d"); $this->udate = $d; }
	
	##########################
	# Getters
	
	public function getId() { return $this->id; }
	public function getTitle() { return $this->title; }
	public function getAuthor() { return $this->author; }
	public function getBody() { return $this->body; }
	public function getExcerpt() { return $this->makeExcerpt(); }
	public function getCreationDate() { return $this->cdate; }
	public function getModificationDate() { return $this->mdate; }
	public function getHash() { return $this->hash; }
	public function getLink() { return $this->link; }
	public function isUsed() { return $this->used; }
	public function getUsageDate() { return $this->udate; }
	
	public function getCategoryId()
	{
		return $this->category_id;
	}
	
	public function getCategoryName()
	{
		$cat = $this->getCategory();
		if($cat) 
			$category = $cat->show('category');
		else 
			$category = "";
		return $category;
	}
	
	public function getCategory()
	{
		$cat = Category::load($this->category_id); // return false is category is null
		if($cat)
			return $cat;
	}
	
	##########################
	# Creates an excerpt from the body
	# Param :
	# Returns :
	public function makeExcerpt()
	{
		if(strlen($this->body) < 1000)
			$l = strlen($this->body);
		else
			$l = 1000;
			
		$this->excerpt = htmlentities( utf8_decode ( substr($this->body, 0, $l) ) );
		return $this->excerpt;
	}
	
	##########################
	# Make a unique hash for this note
	public function makeHash()
	{
		$this->hash = sha1($this->title . $this->author . $this->cdate);
		return $this->hash;
	}
}

?>